
#include "RingQueue.hpp"
#include <time.h>
#include <unistd.h>

void* costomer(void* args)
{
    RingQueue<int>* RQ = (RingQueue<int>*)args;
    while (1)
    {
        sleep(1);
        int data = -1;
        RQ->pop(data);
        std::cout << "已销售: " << data  << " 线程ID: " << pthread_self() << std::endl; 
    }
            
}

void* porducer(void* args)
{
    RingQueue<int>* RQ = (RingQueue<int>*)args;
    while (1)
    {
        int data = rand() % 1000 + 1;
        std::cout << "已生产: " << data <<  " 线程ID: "<< pthread_self() << std::endl;
        
        RQ->push(data);
        sleep(1);
    }
    
}

int main()
{
    pthread_t p[2], c[3];
    srand(time(0) * 23 ^ 0Xeeee);
    RingQueue<int> RQ;    
    pthread_create(p,nullptr, porducer, (void*)&RQ);
    pthread_create(p + 1,nullptr, porducer, (void*)&RQ);
    pthread_create(c,nullptr, porducer, (void*)&RQ);
    pthread_create(c + 1,nullptr, porducer, (void*)&RQ);
    pthread_create(c + 2,nullptr, costomer, (void*)&RQ);

    for (int i  = 0; i < 3; i++)pthread_join(c[i],nullptr);
    for (int i  = 0; i < 2; i++)pthread_join(p[i],nullptr);

    return 0;
}

